﻿using System;
using UnityEngine;
using System.Collections.Generic;
namespace UI
{
    public static class UIRootContainer
    {
        private static List<UIContainer> _uiLayers = new List<UIContainer>();

        private static Dictionary<string, UIForm> _forms = new Dictionary<string, UIForm>();

        private static HashSet<UIForm> _formHashSet = new HashSet<UIForm>();

        private static Stack<UIForm> _formStack = new Stack<UIForm>();

        public static UIForm TopForm
        {
            get { return _formStack.Count > 0 ? _formStack.Peek() : null; }
        }

        internal static void InFormStack(UIForm form)
        {

        }

        internal static void RemoveForm(UIForm form)
        {
            if (form == null) return;
            if (!_formHashSet.Contains(form)) return;
            _formHashSet.Remove(form);
            _forms.Remove(form.Name);
            form.Rect.SetParent(null, false);
        }

        internal static void AddForm(UIForm form)
        {
            if (form == null) return;
            if (_formHashSet.Contains(form)) return;
            _formHashSet.Add(form);
            _forms.Add(form.Name, form);
            var info = form.Info;
            var parent = _uiLayers[info.Layer].transform;
            form.Rect.SetParent(parent, false);
        }

        internal static UIForm FindForm(string formName)
        {
            UIForm form;
            _forms.TryGetValue(formName, out form);
            return form;
        }

        internal static void AddContainer(UIContainer container)
        {
            _uiLayers.Add(container);
        }
    }


}
